package com.yld.car.market;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

import com.yld.car.application.MyApplication;
import com.yld.car.domain.UserInfo;
import com.yld.car.market.tab.TabActivityGroup;

/**
 * 所有页面的基类，它主要提供一些公共的操作
 * 
 * @author apple
 * 
 */
public class BaseActivity extends Activity {

	protected MyApplication mApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// System.out.println("onCreate---"+this);
		mApp = (MyApplication) getApplication();
		mApp.setHistoryStack(this);
	}

	/**
	 * 操作完成时提示框
	 * 
	 * @param param
	 */
	protected void showDialogFinish(String param, final boolean isFinish) {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
		localBuilder.setTitle("提示");
		localBuilder.setMessage(param);
		localBuilder.setCancelable(false);
		localBuilder.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {
						if (isFinish) {
							finish();
						}

					}
				});
		localBuilder.create().show();
	}

	/**
	 * 操作完成时提示框
	 * 
	 * @param param
	 */
	protected void showDialogFinish(String param, final Intent intent) {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
		localBuilder.setTitle("提示");
		localBuilder.setMessage(param);
		localBuilder.setCancelable(false);
		localBuilder.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {

						getApplication().startActivity(intent);
						// finish();
						mApp.exitApp();
					}
				});

		localBuilder.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface paramDialogInterface,
							int paramInt) {

					}
				});
		localBuilder.create().show();
	}

	/**
	 * 定义统一的返回按键设置功能
	 */
	protected void showBackButton() {
		Button localButton = (Button) findViewById(R.id.leftButton);
		localButton.setVisibility(View.VISIBLE);
		localButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				finish();
			}
		});
	}

	/**
	 * 设置自定义标题栏的文本显示。
	 * 
	 * @param resId
	 * @param content
	 */
	protected void settingTitle(int resId, String content) {
		TextView tv = (TextView) findViewById(resId);

		tv.setText(content);
	}

	protected void addCustomColor(View v, String prompt,
			DialogInterface.OnClickListener confrim,
			DialogInterface.OnClickListener cancel) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(prompt);
		if (v != null) {
			builder.setView(v);
		}

		builder.setPositiveButton("确定", confrim);
		builder.setNegativeButton("取消", cancel);
		builder.create().show();
	}

	protected void showLoading(String paramString) {
		this.root = ((ViewGroup) findViewById(R.id.layout_root));
		this.view = LayoutInflater.from(this).inflate(R.layout.loading, null);
		this.view.findViewById(R.id.recorder_ring).setVisibility(View.VISIBLE);
		this.view.setBackgroundResource(R.drawable.pls_talk);
		((TextView) this.view.findViewById(R.id.secondR)).setText(paramString);
		this.menuWindow = new PopupWindow(this.view, 180, 180);
		this.menuWindow.setOutsideTouchable(false);
		this.menuWindow.showAtLocation(this.root, 17, 0, 0);
	}

	protected PopupWindow menuWindow;
	protected String msg;
	// private SysMessageReceiver receiver;
	protected ViewGroup root;
	protected View view;

	protected void hideLoading() {
		if (this.menuWindow != null)
			this.menuWindow.dismiss();
	}

	/**
	 * 对象的序列化
	 */
	protected void writeObject(Object writeObject, String fileName) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = this.openFileOutput(fileName, MODE_PRIVATE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(writeObject);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
	}

	/**
	 * 对象的反序列化
	 * 
	 */

	protected Object readObject(String fileName) {

		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = this.openFileInput(fileName);
			ois = new ObjectInputStream(fis);
			Object o = ois.readObject();

			return o;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	protected void showCancelListener(String text,
			View.OnClickListener cancelClickListener) {
		Button cancel = (Button) findViewById(R.id.rightButton);
		cancel.setVisibility(View.VISIBLE);
		cancel.setText(text);
		cancel.setOnClickListener(cancelClickListener);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		JPushInterface.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(this);

	}
}
